﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Text;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.TreeSharp;
using Styx.Helpers;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Pathing;
using Styx.CommonBot.Routines;

using CommonBehaviors.Actions;

using Action = Styx.TreeSharp.Action;


namespace DWCC
{
    public partial class Warrior : CombatRoutine
    {
        public override sealed string Name { get { return "Dunatank's Warrior CC v5.3 BT - Arms and Prot fix by SD"; } }
        public override WoWClass Class { get { return WoWClass.Warrior; } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }
        private static Version ver { get { return new Version(5, 3, 3); } }

        #region Initialize
        public override void Initialize()
        {
            Logging.Write("[DWCC]: Dunatank's Warrior CC v5.3 BT will do the job for you now that SD fixed it for himself and Nate!");
            Logging.Write("[DWCC]: created by Wownerds Dev Team and fixed by SD!");
            CharacterSettings.Instance.PullDistance = 24;
            Logging.WriteDiagnostic("[DWCC]: ###Character Information###");
            Logging.WriteDiagnostic("[DWCC]: Level: " + Me.Level);
            Logging.WriteDiagnostic("[DWCC]: Alliance/Horde: " + Me.IsAlliance + " || " + Me.IsHorde);
            Logging.WriteDiagnostic("[DWCC]: Health: " + Me.MaxHealth);
            Logging.WriteDiagnostic("[DWCC]: Movement Disabled: " + DunatanksSettings.Instance.DisableMovement);
            Logging.WriteDiagnostic("[DWCC]: Pull: " + !DunatanksSettings.Instance.DisableMovement);
            Logging.WriteDiagnostic("[DWCC]: CC Specc: " + DunatanksSettings.Instance.useArms + " || " + DunatanksSettings.Instance.useFury + " || " + DunatanksSettings.Instance.useProt);
            Logging.WriteDiagnostic("[DWCC]: ###Character Information###");
            if (Me.Specialization == WoWSpec.WarriorArms)
            {
                DunatanksSettings.Instance.useArms = true;
                DunatanksSettings.Instance.useFury = false;
                DunatanksSettings.Instance.useProt = false;
            }
            else if (Me.Specialization == WoWSpec.WarriorFury)
            {
                DunatanksSettings.Instance.useArms = false;
                DunatanksSettings.Instance.useFury = true;
                DunatanksSettings.Instance.useProt = false;
            }
            else if (Me.Specialization == WoWSpec.WarriorProtection)
            {
                DunatanksSettings.Instance.useArms = false;
                DunatanksSettings.Instance.useFury = false;
                DunatanksSettings.Instance.useProt = true;
            }
        }
        #endregion
        #region Pulse
        public override void Pulse()
        {
            MoveOutOfAoE();
            LifeSpiritRegen();
            if ((Me.GroupInfo.IsInParty || Me.GroupInfo.IsInRaid) && !Battlegrounds.IsInsideBattleground && DunatanksSettings.Instance.useAutoTargetProt && DunatanksSettings.Instance.useProt)
            {
                TankManager.Instance.Pulse();
                if (TankManager.Instance.FirstUnit != null) TankManager.Instance.FirstUnit.Target();
            }
            if (Me.CurrentTarget != null)
                if(!Me.CurrentTarget.IsAlive || Me.CurrentTarget.IsFriendly && !DunatanksSettings.Instance.DisableMovement) Me.ClearTarget();
        }
        #endregion
        #region Buttons
        public override bool WantButton
        {
            get
            {
                return true;
            }
        }
        public override void OnButtonPress()
        {
            DWCC.cfg cfg = new DWCC.cfg();
            cfg.ShowDialog();
        }
        #endregion
        #region Rest
        public override bool NeedRest
        {
            get
            {
                if (Me.HealthPercent <= DunatanksSettings.Instance.RestPercent && DunatanksSettings.Instance.UseRest)
                {
                    return true;
                }
                else
                    return false;
            }
        }

        public override void Rest()
        {
            if (Me.HealthPercent <= DunatanksSettings.Instance.RestPercent && DunatanksSettings.Instance.UseRest && !Me.IsFlying && !Me.IsSwimming)
            {
                Styx.CommonBot.Rest.Feed();
            }

        }
        #endregion
        #region Combat
        private Composite _combatBehavior;

        public override Composite CombatBehavior
        {
            get { if (_combatBehavior == null) { Logging.Write("[DWCC]: Creating DWCC MoP Combat Behavior."); _combatBehavior = CreateCombatBehavior(); } return _combatBehavior; }
        }

        private Composite CreateCombatBehavior()
        {
            return CreateDWCCBehavior();
        }
        #endregion
        #region Pull
        private Composite _pullBehavior;
        public override Composite PullBehavior
        {
            get
            {
                if (_pullBehavior == null)
                {
                    Logging.Write("[DWCC]: Creating 'Pull' behavior");
                    _pullBehavior = CreatePullBehavior();
                }
                _pullBehavior = CreatePullBehavior();
                return _pullBehavior;
            }
        }
        private PrioritySelector CreatePullBehavior()
        {
            return new PrioritySelector(
                //new Action(a => { Logging.Write("Pull"); return RunStatus.Failure;}),
                new Decorator(ret => Me.Mounted, new Action(a => { Mount.Dismount("Pull"); return RunStatus.Failure; })),
                FaceTarget(),
                CastCharge(),
                HeroicLeap(),
                CreateSpellCheckAndCast("Heroic Throw", ret => true),
                /*new Action(a => { Logging.Write("Hostile: " + Me.CurrentTarget.IsHostile);
                                    Logging.Write("Disable Movement: " + !DunatanksSettings.Instance.DisableMovement);
                                    Logging.Write("Target out of Melee: " + Me.CurrentTarget.Distance + "/" + MeleeDistance(Me.CurrentTarget));
                                    Logging.Write("Last Movement: " + (DateTime.Now - LastMovement));
                                    Logging.Write("Distance from last MovePoint: " + LastMovementPoint.Distance(Me.CurrentTarget.Location));
                                    return RunStatus.Failure; }),*/
                MoveToTarget(),
                CreateAutoAttack());
        }
        #endregion
        #region Resting
        public override Composite RestBehavior
        {
            get
            {
                return new Decorator(ret => !Me.IsDead && !Me.Combat && !Me.IsMoving && !Me.IsGhost && !Me.IsSwimming && Me.HealthPercent < 40,
                    new Action(ret =>
                        {
                            if (!Me.HasAura("Food")) Styx.CommonBot.Rest.FeedImmediate();
                            if (Me.HasAura("Food") && Me.HealthPercent < 90) return RunStatus.Running;
                            else return RunStatus.Success;
                        }));
            } 
        }
        #endregion
    }
}